package top.infopub.mgr.mkt.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.mkt.dao.ProAcceptDao;
import top.infopub.mgr.mkt.service.ProAcceptService;
import top.infopub.mgr.mkt.vo.ProAcceptVo;
import top.infopub.model.mkt.ProAccept;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;

@Service
public class ProAcceptServiceImpl extends GenericServiceImpl<ProAccept, String> implements
		ProAcceptService {

	@Autowired
	private ProAcceptDao proAcceptDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<ProAccept, String> getDao() {
		return proAcceptDao;
	}

    @Override
    public Page<ProAcceptVo> selectPageByDomainWhere(Page<ProAcceptVo> page,
                                                     ProAcceptVo proAcceptVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProAcceptVo>)proAcceptDao.selectByDomainWhere(proAcceptVo);
    }

    @Override
    public Page<ProAcceptVo> selectMyCheckByDomainWhere(Page<ProAcceptVo> page,
                                                        ProAcceptVo proAcceptVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProAcceptVo>)proAcceptDao.selectMyCheckByDomainWhere(proAcceptVo);
    }

    @Override
    public ProAcceptVo selectById(String id) {
        return proAcceptDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProAccept(ProAccept proAccept) {
        //插入预立项记录
        proAcceptDao.insertById(proAccept);
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.MKT_PRO_ACCEPT;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, proAccept.getId(), proAccept.getProcessId(), null);
        
    }

    @Override
    public Page<ProAcceptVo> selectDataByWhere(Date dateStart, Date dateEnd, String sgCityName,
                                               String orgProName, Page<ProAcceptVo> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Map<String, Object> where = Maps.newHashMap();
        where.put("noticeDateStart", dateStart);
        where.put("noticeDateEnd", dateEnd);
        where.put("sgCityName", sgCityName);
        where.put("proName", orgProName);
        return (Page<ProAcceptVo>)proAcceptDao.selectDataByWhere(where);
    }

    @Override
    public List<ProAccept> selectByProCode(String proCode) {
        return proAcceptDao.selectByProCode(proCode);
    }
}